(function() {
    'use strict';

    angular.module('weApp.controllers')

            //问题反馈
            .controller('questionCtrl', ['$scope', '$ionicPopup', '$rootScope', '$state', '$stateParams', '$timeout', '$http', 'weAppDataBase', 'showLinePageService', 'localStorageService', 'myFavorsService',
                function($scope, $ionicPopup, $rootScope, $state, $stateParams, $timeout, $http, weAppDataBase, showLinePageService, localStorageService, myFavorsService) {

                    var resize = function(image, scale, type) {
                        var canvas = document.createElement('canvas');
                        canvas.width = image.width * scale;
                        canvas.height = image.height * scale;
                        var context = canvas.getContext('2d');
                        context.clearRect(0, 0, image.width, image.height);
                        context.drawImage(image, 0, 0, canvas.width, canvas.height);
                        var data = canvas.toDataURL(type);
                        return base64ToBlob(data);
                    }

                    /**
                     *
                     * @type {tools.base64ToBlob}
                     */
                    var base64ToBlob = function(urlData) {
                        var mimeString = urlData.split(',')[0].split(':')[1].split(';')[0]; // mime类型
                        var byteString = atob(urlData.split(',')[1]); //base64 解码
                        var arrayBuffer = new ArrayBuffer(byteString.length); //创建缓冲数组
                        var intArray = new Uint8Array(arrayBuffer); //创建视图
                        for (var i = 0; i < byteString.length; i += 1) {
                            intArray[i] = byteString.charCodeAt(i);
                        }
                        var blob = new Blob([intArray], {type: mimeString}); //转成blob
                        return blob;
                    }
                    //图片预览

                    $("#file").change(function() {
                        var objUrl = getObjectURL(this.files[0]);
                        if (objUrl) {
                            $("#show").attr("src", objUrl).show();
                        }
                    });

                    function getObjectURL(file) {
                        var url = null;
                        if (window.createObjectURL != undefined) { // basic
                            url = window.createObjectURL(file);
                        } else if (window.URL != undefined) { // mozilla(firefox)
                            url = window.URL.createObjectURL(file);
                        } else if (window.webkitURL != undefined) { // webkit or chrome
                            url = window.webkitURL.createObjectURL(file);
                        }
                        return url;
                    }

                    //图片缩放
                    $scope.scale = function(args) {
                        var oImg = document.getElementById("show");
                        if (oImg.src) {
                            if (args) {
                                oImg.width = oImg.width * 1.1;
                                oImg.height = oImg.height * 1.1;
                            }
                            else {
                                oImg.width = oImg.width / 1.1;
                                oImg.height = oImg.height / 1.1;
                            }

                        }

                    }
                    $scope.tag = true;
                    $scope.quest = function() {
                        if ($scope.tag) {
                            $scope.tag = false;
                        } else {
                            return false;
                        }
                        //单验证
                        var question = $scope.feedback;
                        var qq = $scope.contactMethod;
                        if (!question) {
                            var myPopup = $ionicPopup.show({
                                title: "温馨提示",
                                template: "亲,请您写点意见吧",
                                cssClass: "validate"
                            });
                        } else if (!qq) {
                            var myPopup = $ionicPopup.show({
                                title: "温馨提示",
                                template: "亲,请留个联系方式吧",
                                cssClass: "validate"
                            });
                        }
                        if (myPopup) {
                            $timeout(function() {
                                myPopup.close();
                            }, 2000);
                            return false;
                        }
                        if ($("#file").val()) {
                            var data = new FormData($('#form')[0]);
                            var url = window.URL.createObjectURL($("#file")[0].files[0]);
                            $scope.myPopu = $ionicPopup.show({
                                title: "提交中",
                                cssClass: "validate"
                            });

                            $('#hide_img').one('load', function() {
                                for (var i = 0; i < 10; i++) {

                                    var imagedata = resize(this, (10 - (i + 1)) / 10, 'image/png');
                                    if (imagedata.size <= 1024 * 1024) {
                                        var image = imagedata;
                                        var formData = new FormData();
                                        var names = $("#file").val().split("\\");
                                        names = names[names.length - 1];
                                        //+"." + mimeType[imagedata.type]
                                        formData.append($("#file")[0].name, imagedata, names);
                                        formData.append("cityName", $rootScope.cityName);
                                        formData.append("cityCode", $rootScope.cityCode);
                                        formData.append("feedback", $scope.feedback);
                                        formData.append("contactMethod", $scope.contactMethod);

                                        $.ajax({
                                            contentType: false,
                                            processData: false,
                                            url: $rootScope.rootLocation + '/line/bus/new/feedback',
                                            type: 'POST',
                                            data: formData,
                                            success: function(res) {
                                                $scope.tag = true;
                                                $scope.myPopu.close();
                                                if (res.head.success == "true") {
                                                    var myPopu = $ionicPopup.show({
                                                        title: "提交成功",
                                                        cssClass: "validate"
                                                    });

                                                    $timeout(function() {
                                                        myPopu.close();
                                                    }, 1000);
                                                    window.history.back();
                                                } else {
                                                    var myPopu = $ionicPopup.show({
                                                        title: "提交失败",
                                                        cssClass: "validate"
                                                    });
                                                    $timeout(function() {
                                                        myPopu.close();
                                                    }, 1000);
                                                }
                                            },
                                            error: function(res) {
                                                $scope.tag = true;
                                                $scope.myPopu.close();
                                                var myPopu = $ionicPopup.show({
                                                    title: "提交失败",
                                                    cssClass: "validate"
                                                });
                                                $timeout(function() {
                                                    myPopu.close();
                                                }, 1000);
                                            }
                                        });
                                        break;
                                    }


                                }


                            });

                            $('#hide_img').attr("src", url);

                        } else {
                            // var rootReq = {
                            //     method: 'post',
                            //     url: $rootScope.rootLocation + '/line/bus/new/feedback',
                            //     data: {
                            //         "cityName": $rootScope.cityName,
                            //         "cityCode": $rootScope.cityCode,
                            //         "feedback": $scope.feedback,
                            //         "contactMethod": $scope.contactMethod
                            //     }
                            // };
                            //
                            // $http(rootReq).then(function (data) {
                            //     if (data.data.head.success == "true") {
                            //         var myPopu = $ionicPopup.show({
                            //             title: "温馨提示",
                            //             template: "反馈意见提交成功",
                            //             cssClass: "validate"
                            //         });
                            //         $timeout(function () {
                            //             myPopu.close();
                            //         }, 2000);
                            //         window.history.back();
                            //     }
                            //
                            // });
                            $.ajax({
                                url: $rootScope.rootLocation + '/line/bus/new/feedback',
                                type: 'POST',
                                data: {
                                    "cityName": $rootScope.cityName,
                                    "cityCode": $rootScope.cityCode,
                                    "feedback": $scope.feedback,
                                    "contactMethod": $scope.contactMethod
                                },
                                success: function(res) {
                                    if (res.head.success == "true") {
                                        var myPopu = $ionicPopup.show({
                                            title: "提交成功",
                                            cssClass: "validate"
                                        });
                                        $timeout(function() {
                                            myPopu.close();
                                            window.history.back();
                                        }, 1000);

                                    } else {
                                        var myPopu = $ionicPopup.show({
                                            title: "提交失败",
                                            cssClass: "validate"
                                        });
                                        $timeout(function() {
                                            myPopu.close();
                                        }, 1000);
                                    }
                                    $scope.tag = true;
                                },
                                error: function(res) {
                                    $scope.tag = true;
                                }
                            });
                        }


                        // var myPopu = $ionicPopup.show({
                        //     title: "温馨提示",
                        //     template: "反馈意见提交成功",
                        //     cssClass: "validate"
                        // });
                        // $timeout(function () {
                        //     myPopu.close();
                        // }, 2000);
                        // window.history.back();


                    }
                    $scope.look = function() {
                        $('#file').click();
                    }
                }]);

})();